package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SupervisorVo;
import com.prd.mapper.EntGoalPeriodicMapper;
import com.prd.model.EntGoalPeriodic;
import com.prd.model.LoginUser;
import com.prd.service.EntGoalPeriodicService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntGoalPeriodicServiceImpl extends ServiceImpl<EntGoalPeriodicMapper, EntGoalPeriodic> implements EntGoalPeriodicService {

    @Autowired
    private EntGoalPeriodicMapper entGoalPeriodicMapper;

    @Override
    public int insertInfo(EntGoalPeriodic entGoalPeriodic) {
        LambdaQueryWrapper<EntGoalPeriodic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntGoalPeriodic::getGoalId, entGoalPeriodic.getGoalId());
        queryWrapper.eq(entGoalPeriodic.getStageId() != null, EntGoalPeriodic::getStageId, entGoalPeriodic.getStageId());
        EntGoalPeriodic goalPeriodic = entGoalPeriodicMapper.selectOne(queryWrapper);
        if (goalPeriodic != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entGoalPeriodic);
        return entGoalPeriodicMapper.insert(entGoalPeriodic);
    }

    @Override
    public int updateInfo(EntGoalPeriodic entGoalPeriodic) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entGoalPeriodic);
        return entGoalPeriodicMapper.updateById(entGoalPeriodic);
    }

    @Override
    public int addSupervisor(SupervisorVo supervisorVo) {
        EntGoalPeriodic entGoalPeriodic = entGoalPeriodicMapper.selectById(supervisorVo.getId());
        if (entGoalPeriodic == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entGoalPeriodic.setReach(supervisorVo.getReach());
        entGoalPeriodic.setGist(supervisorVo.getGist());
        entGoalPeriodic.setContent(supervisorVo.getContent());
        entGoalPeriodic.setRecord(supervisorVo.getRecord());
        entGoalPeriodic.setPicture(supervisorVo.getPicture());
        return updateInfo(entGoalPeriodic);
    }
}
